package annotation;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * User: heihaier@xfuweng.com
 * Date: 2015/7/3
 * Time: 14:52
 */
public class PersistStudent {

    public static void main(String[] args) {

        try {

            Class clazz = Class.forName("annotation.Student");
            Object o = clazz.newInstance();
            Method[] declaredMethods = clazz.getDeclaredMethods();
            ValueBind annotation = null;
            String type = null;
            String value = null;

            for (Method method : declaredMethods) {

                if (method.isAnnotationPresent(ValueBind.class)) {

                    annotation = method.getAnnotation(ValueBind.class);
                    type = String.valueOf(annotation.type());
                    value = annotation.value();

                    if ("INT".equals(type)) {

                        method.invoke(o, new Integer[]{Integer.parseInt(value)});
                    } else {
                        method.invoke(o, new String[]{value});
                    }
                }

            }

            Student annotaedStudent = (Student) o;
            System.out.println("  studentnName====" + annotaedStudent.getName() + "   student Age====" + annotaedStudent.getAge());
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
